package com.coocaa.internship.service.serviceImpl;

import com.coocaa.internship.common.MD5;
import com.coocaa.internship.pojo.UserLogin;
import com.coocaa.internship.service.AuthService;
import com.coocaa.internship.util.RedisUtil;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Author: 凌贤
 * @Date: 2019-07-29
 * @Time: 14:23
 */
@Service
public class AuthServicelmpl implements AuthService {
    @Autowired
    RedisUtil redisUtil;

    @Override
    public boolean tokenAuthen(@NonNull Long userId, @NonNull String token) {
        if (token.equals(redisUtil.get(String.valueOf(userId)))) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean reflushToken(@NonNull UserLogin userLogin) {
        return redisUtil.expire(String.valueOf(userLogin.getUserId()), 1800);
    }

    @Override
    public String creatToken(@NonNull UserLogin userLogin) {
        String token;
        token = MD5.md5(userLogin.getUserId() + userLogin.getUserPassword() + System.currentTimeMillis());
        redisUtil.set(String.valueOf(userLogin.getUserId()), token);
        return token;
    }
}
